package com.common;


import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;


public class Password {
	 public static final String HEXSTRING = "0123456789ABCDEF";  
	public String encry(String v1,String v2,String v3,String v4){
		String ret = "" ;
		
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("javascript");
		String jsFileName = "lib/a.js";  
		FileReader reader;
		try {
			reader = new FileReader(jsFileName);
			engine.eval(reader);
			
			if (engine instanceof Invocable) {
				Invocable invoke = (Invocable) engine;
				ret = (String)invoke.invokeFunction("Encrypt", "", "", v1, v2, v3,v4);
			}
			reader.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		
		return ret ;
	}
	
	public String encryall(String v1,String v2,String v3){
		String ret = "" ;
		
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("javascript");
		String jsFileName = "lib/a.js";  
		FileReader reader;
		try {
			reader = new FileReader(jsFileName);
			engine.eval(reader);
			
			if (engine instanceof Invocable) {
				Invocable invoke = (Invocable) engine;
				ret = (String)invoke.invokeFunction("encryAll", v1, v2, v3);
			}
			reader.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		
		return ret ;
	}
	
	public String encrynew(String v1,String v2,boolean v3){
		String ret = "" ;
		
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("javascript");
		String jsFileName = "js/snew.js";  
		FileReader reader;
		try {
			reader = new FileReader(jsFileName);
			engine.eval(reader);
			
			if (engine instanceof Invocable) {
				Invocable invoke = (Invocable) engine;
				ret = (String)invoke.invokeFunction("getRSAEncryption", v1, v2, v3);
			}
			reader.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		
		return ret ;
	}
	
	
	public static String GetPassword(String qq, String password,  
            String verifycode) throws Exception  
    {  
        String P = hexchar2bin(md5(password));  
        String U = md5(P + hexchar2bin(qq.replace("\\x", "").toUpperCase()));  
        String V = md5(U + verifycode.toUpperCase());  
        return V;  
    }  
      
    public static String md5(String originalText) throws Exception  
    {  
        byte buf[] = originalText.getBytes("ISO-8859-1");  
        StringBuffer hexString = new StringBuffer();  
        String result = "";  
        String digit = "";  
        try  
        {  
            MessageDigest algorithm = MessageDigest.getInstance("MD5");  
            algorithm.reset();  
            algorithm.update(buf);  
            byte[] digest = algorithm.digest();  
            for (int i = 0; i < digest.length; i++)  
            {  
                digit = Integer.toHexString(0xFF & digest[i]);  
                if (digit.length() == 1)  
                {  
                    digit = "0" + digit;  
                }  
                hexString.append(digit);  
            }  
            result = hexString.toString();  
        }  
        catch (Exception ex)  
        {  
            result = "";  
        }  
        return result.toUpperCase();  
    }  
  
    public static String hexchar2bin(String md5str) throws UnsupportedEncodingException  
    {  
        ByteArrayOutputStream baos = new ByteArrayOutputStream(md5str.length() / 2);  
        for (int i = 0; i < md5str.length(); i = i + 2)  
        {  
            baos.write((HEXSTRING.indexOf(md5str.charAt(i)) << 4 | HEXSTRING  
                    .indexOf(md5str.charAt(i + 1))));  
        }  
        return new String(baos.toByteArray(), "ISO-8859-1");  
    }  
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//String ss = org.apache.commons.lang.StringEscapeUtils.unescapeJava("\\x00\\x00\\x00\\x00\\x8d\\x59\\xfe\\xa4".replace("\\x", "\\u00"));
//		String[] ss = "\\x00\\x00\\x00\\x00\\x15\\xc9\\x0f\\xc6".split("\\x");
//		byte[] bs = new byte[ss.length];
//
//		for(int i=0;i<ss.length;i++){
//		bs[i] = (byte)Integer.parseInt(ss[i], 16);
//		}
//		String s ;
//		try {
//			s =new String(bs, "utf-8");
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} 
//		System.out.println(s);
		Password p = new Password() ;
//		String r = p.encry("preprocess", "691516", "!HUV") ;
//		System.out.println(r);
		//var randomNum="a75fcdd0b39fa37c72323812f3b36f69ffc2f8fdc06944b8114c62e7c8d0b292cf2c0d2616d8724c78ac13449b52a1dea7c264e48d3a175f5dbb618277ddc2558e9ebedb3908cde1074ccd2f9c42624b2154148d1178ad920179086a8605834a9a383ae8"; 
		 try {
			//String r =p.encrynew("2451850079", "!HFC",false) ;
			 String r = p.encry("newpwd", "Cxh_1314","a75fcdd0b39fa37c72323812f3b36f69ffc2f8fdc06944b8114c62e7c8d0b292cf2c0d2616d8724c78ac13449b52a1dea7c264e48d3a175f5dbb618277ddc2558e9ebedb3908cde1074ccd2f9c42624b2154148d1178ad920179086a8605834a9a383ae8",
					 "e=10001;m=a60f1b66ef4023496137ee371eb24b307642d7351335bc9055c1cd1b6d67ad1b38119579f61276ea82e35e370208567a9667e6cec28df2a3af1a0f64f51405551fe8cd4f92fe103547dd1f75ec64650c4274c9391d394a747b9d0bdc4774ce02ef81c7492ab011734f1adb5f39155ad7d80cb80243024f356be1812f23a043bd");
			System.out.println(r);
			
			 String r1 = p.encryall("Cxh_1314","a75fcdd0b39fa37c72323812f3b36f69ffc2f8fdc06944b8114c62e7c8d0b292cf2c0d2616d8724c78ac13449b52a1dea7c264e48d3a175f5dbb618277ddc2558e9ebedb3908cde1074ccd2f9c42624b2154148d1178ad920179086a8605834a9a383ae8",
					 "e=10001;m=a60f1b66ef4023496137ee371eb24b307642d7351335bc9055c1cd1b6d67ad1b38119579f61276ea82e35e370208567a9667e6cec28df2a3af1a0f64f51405551fe8cd4f92fe103547dd1f75ec64650c4274c9391d394a747b9d0bdc4774ce02ef81c7492ab011734f1adb5f39155ad7d80cb80243024f356be1812f23a043bd");
			System.out.println(r1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
//		DCEC771AC66853F809EFF7D4B68C861F
//		CB1E1D22A9F130530792C45FF09FAFE0

	}

}
